module WWW
  class Tkd
    class Stat
      #根据用户ID将其
      def self.company_seller user_id
        sql = "select t2.agent_id ,t2.no_code from tb_seller_orders t1,expresses t2 where t1.user_id="+user_id.to_s+" and t1.express_id=t2.id and t2.is_end=1"
        record_express = ActiveRecord::Base.connection.execute(sql)
        hash_express =[]
        record_express.each_hash { |item| hash_express << item}
        result =[]
        hash_express.each do |item|
          agent_id = item["agent_id"]
          no_code=item["no_code"]
          sql = "select time_format(timediff(max(happen_time),min(happen_time)),'%H') as value from statuses where agent_id="+agent_id.to_s+" and no='"+no_code+"'"
          record_time = ActiveRecord::Base.connection.execute(sql)
          hash_time=[]
          record_time.each_hash { |item| hash_time << item}
          if hash_time.size>0
            time = hash_time[0]["value"]
            item = Hash.new
            item["agent_id"]=agent_id
            item["no_code"]=no_code
            item["time"]=time.to_i
          end
        end
      end
    end
  end
end
